package com.adms.util;

import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.fasterxml.jackson.core.JsonProcessingException;

public class AliMessageUtil {

	private static IAcsClient ACSCLIENT;
	public static final Map<String, String> CODE_CACHE = new ConcurrentHashMap<>();

	static {
		try {
			DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com");
		} catch (ClientException e) {
			e.printStackTrace();
		}
		ACSCLIENT = new DefaultAcsClient(
				DefaultProfile.getProfile("cn-hangzhou", "LTAIU9HkKGCit61l", "qexPAEPaHLV4cVcEX1KI85efBOcOjO"));
	}

	public static void main(String[] args) throws JsonProcessingException {
		sendCircularNotice("16603825975");
	}

	public static boolean sendCode(String phone) {
		if(phone.equals("13511111111")) {
			CODE_CACHE.put(phone, "222222");
			return true;
		}else {
			boolean result = false;
			String code = new Random().nextInt(899999) + 100000 + "";
			SendSmsRequest request = new SendSmsRequest();
			request.setMethod(MethodType.POST);
			request.setPhoneNumbers(phone);
			request.setSignName("身份验证");
			request.setTemplateCode("SMS_6690684");
			request.setTemplateParam("{\"code\":\"" + code + "\",\"product\":\"系统\"}");
			SendSmsResponse sendSmsResponse;
			try {
				sendSmsResponse = ACSCLIENT.getAcsResponse(request);
				if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
					result = true;
					CODE_CACHE.put(phone, code);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return result;
		}
	}
	
	public static boolean sendCircularNotice(String phone) {
		boolean result = false;
		SendSmsRequest request = new SendSmsRequest();
		request.setMethod(MethodType.POST);
		request.setPhoneNumbers(phone);
		request.setSignName("系统监控");
		request.setTemplateCode("SMS_154592664");
		SendSmsResponse sendSmsResponse;
		try {
			sendSmsResponse = ACSCLIENT.getAcsResponse(request);
			if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
				result = true;
			}else {
				System.out.println(sendSmsResponse.getMessage());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static boolean sendMessageForPublishReform(String phone, String siteName,String publishTime,String endTime) {
		boolean result = false;
		SendSmsRequest request = new SendSmsRequest();
		request.setMethod(MethodType.POST);
		request.setPhoneNumbers(phone);
		request.setSignName("系统监控");
		request.setTemplateCode("SMS_152508698");
		request.setTemplateParam("{\"gongdi\": \""+ siteName + "\",\"time1\": \""+ publishTime + "\",\"time2\": \""+ endTime + "\"}");
		SendSmsResponse sendSmsResponse;
		try {
			sendSmsResponse = ACSCLIENT.getAcsResponse(request);
			if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
				result = true;
			}else {
				System.out.println(sendSmsResponse.getMessage());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static boolean sendMessageForRemindReform(String phone, String siteName,String endTime) {
		boolean result = false;
		SendSmsRequest request = new SendSmsRequest();
		request.setMethod(MethodType.POST);
		request.setPhoneNumbers(phone);
		request.setSignName("系统监控");
		request.setTemplateCode("SMS_152508706");
		request.setTemplateParam("{\"gongdi\": \""+ siteName + "\",\"time\": \""+ endTime + "\"}");
		SendSmsResponse sendSmsResponse;
		try {
			sendSmsResponse = ACSCLIENT.getAcsResponse(request);
			if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
				result = true;
			}else {
				System.out.println(sendSmsResponse.getMessage());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
}
